package avicti.bdp.das;

import avicit.bdp.common.service.dto.DataSourceDTO;
import avicit.platform6.core.rest.msg.ResponseMsg;
import avicti.bdp.TestUtils;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.util.LinkedHashMap;
import java.util.List;

import static org.junit.Assert.assertEquals;

public class DatasourceTest {
    private RestTemplate restTemplate = new RestTemplate();

    private static String datasourceName = "ds_" + System.currentTimeMillis();
    private static String typeId = "typeId_" + System.currentTimeMillis();

    /**
     * 所有用例执行前准备工作
     */
    @BeforeClass
    public static void init() {
        System.out.println("用例初始化...");
    }

    /**
     * 单个测试用例运行前准备工作
     */
    @Before
    public void setUp() {
        // TODO:
    }

    /**
     * 单个测试用例运行后销毁工作
     */
    @After
    public void tearDown() {
        // TODO:
    }

    /**
     * 所有用例执行完成后准备工作
     */
    @AfterClass
    public static void destory() {
        System.out.println("用例执行完成，开始清理...");
    }

    /***********************************************Begin 测试用例 ***********************************************/
    @Test(timeout = 5 * 60 * 1000)
    public void testFile() throws Exception {
        // 新增数据源
        JSONObject param = new JSONObject();
        param.put("datasourceName", datasourceName);
        param.put("datasourceStructure", 0);
        param.put("datasourceType", "mysql");
        param.put("jdbcUrl", "");
        param.put("projectType", 0);
        param.put("typeId", typeId);
        param.put("connectionParams", "{\"user\":\"root\",\"password\":\"rootroothdp\",\"ip\":\"192.168.0.22\",\"port\":\"3306\",\"database\":\"wm\",\"type\":\"mysql\"}");

        ResponseEntity<ResponseMsg> result = restTemplate.postForEntity(TestUtils.HOSTPORT + "/api/bdp/dms/das/dataSource/insertDataSource",
                TestUtils.buildPostHttpEntity(param.toJSONString()), ResponseMsg.class);

        TestUtils.handleFailedRequest(result);
        assertEquals("200", result.getBody().getRetCode());

        // 获取列表
        result = restTemplate.exchange(TestUtils.HOSTPORT + "/api/bdp/dms/das/dataSource/searchDataSource?typeId=" + typeId,
                HttpMethod.GET, TestUtils.buildGetHttpEntity(), ResponseMsg.class);

        TestUtils.handleFailedRequest(result);
        assertEquals("200", result.getBody().getRetCode());

        LinkedHashMap<String, Object> map = (LinkedHashMap<String, Object>) result.getBody().getResponseBody();
        List<Object> finalBody = (List<Object>) map.get("result");
        String dataSourceId = "";
        DataSourceDTO dataSource = null;
        for (Object obj : finalBody) {
            LinkedHashMap<String, String> templateObj = (LinkedHashMap<String, String>) obj;
            dataSource = JSON.parseObject(JSON.toJSONString(templateObj), DataSourceDTO.class);
            if (dataSource.getDatasourceName().equals(datasourceName)) {
                System.out.println("数据源名称:" + dataSource.getDatasourceName());
                dataSourceId = dataSource.getId();
                break;
            }
        }

        // 修改数据源
        if(dataSource != null){
            result = restTemplate.postForEntity(TestUtils.HOSTPORT + "/api/bdp/dms/das/dataSource/updateDataSource",
                    TestUtils.buildPostHttpEntity(JSON.toJSONString(dataSource)), ResponseMsg.class);

            TestUtils.handleFailedRequest(result);
            assertEquals("200", result.getBody().getRetCode());
        }

        // 删除数据源
        JSONObject param1 = new JSONObject();
        param1.put("id", dataSourceId);
        result = restTemplate.postForEntity(TestUtils.HOSTPORT + "/api/bdp/dms/das/dataSource/deleteDataSource",
                TestUtils.buildPostHttpEntity(param1.toJSONString()), ResponseMsg.class);

        TestUtils.handleFailedRequest(result);
        assertEquals("200", result.getBody().getRetCode());
    }

}
